const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    hidden: true
  },
  clickCity(e){
    console.log(e.currentTarget.dataset.city);
    let city = e.currentTarget.dataset.city;
    app.globalData.selectedCity = city;
    wx.navigateBack();
  },
  onLoad() {
    // let list = [];
    // for (let i = 0; i < 26; i++) {
    //   list[i] = String.fromCharCode(65 + i)
    // }
    let list = [
      {
        key:"X",
        value:[
          {
            id: 1,
            name: "西安"
          },
          {
            id: 11,
            name: "西宁"
          }
        ]
      },
      {
        key: "S",
        value: [
          {
            id: 2,
            name: "上海"
          },
          {
            id: 21,
            name: "商洛"
          },
          {
            id: 22,
            name: "上海"
          },
          {
            id: 23,
            name: "商洛"
          },
          {
            id: 24,
            name: "上海"
          },
          {
            id: 25,
            name: "商洛"
          },
          {
            id: 26,
            name: "上海"
          },
          {
            id: 27,
            name: "商洛"
          }
        ]
      },
      {
        key: "H",
        value: [
          {
            id: 3,
            name: "合肥"
          },
          {
            id: 31,
            name: "淮南"
          },
          {
            id: 32,
            name: "衡阳"
          },
          {
            id: 33,
            name: "惠阳"
          },
          {
            id: 34,
            name: "黄山"
          }
        ]
      }
    ]
    this.setData({
      list: list,
      listCur: list[0].value[0].name
    })
  },
  onReady() {
    let that = this;
    wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function (res) {
      that.setData({
        boxTop: res.top
      })
    }).exec();
    wx.createSelectorQuery().select('.indexes').boundingClientRect(function (res) {
      that.setData({
        barTop: res.top
      })
    }).exec()
  },
  //获取文字信息
  getCur(e) {
    this.setData({
      hidden: false,
      listCur: e.target.id,
    })
  },

  setCur(e) {
    this.setData({
      hidden: true,
      listCur: this.data.listCur
    })
  },
  //滑动选择Item
  tMove(e) {
    let y = e.touches[0].clientY,
      offsettop = this.data.boxTop,
      that = this;
    //判断选择区域,只有在选择区才会生效
    if (y > offsettop) {
      let num = parseInt((y - offsettop) / 20);
      this.setData({
        listCur: that.data.list[num].key
      })
    };
  },

  //触发全部开始选择
  tStart() {
    this.setData({
      hidden: false
    })
  },

  //触发结束选择
  tEnd() {
    this.setData({
      hidden: true,
      listCurID: this.data.listCur
    })
  },
  indexSelect(e) {
    let that = this;
    let barHeight = this.data.barHeight;
    let list = this.data.list;
    let scrollY = Math.ceil(list.length * e.detail.y / barHeight);
    for (let i = 0; i < list.length; i++) {
      if (scrollY < i + 1) {
        that.setData({
          listCur: list[i].key,
          movableY: i * 20
        })
        return false
      }
    }
  }
});